<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>传感器数据分析系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            padding: 40px;
            max-width: 600px;
            width: 100%;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
        }

        .header h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
        }

        .header p {
            color: #666;
            font-size: 16px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
            font-size: 14px;
        }

        .form-group input,
        .form-group select {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s ease;
            background-color: #f8f9fa;
        }

        .form-group input:focus,
        .form-group select:focus {
            outline: none;
            border-color: #667eea;
            background-color: white;
        }

        .submit-btn {
            width: 100%;
            padding: 14px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: transform 0.2s ease;
            margin-top: 10px;
        }

        .submit-btn:hover {
            transform: translateY(-2px);
        }

        .submit-btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .result {
            margin-top: 30px;
            padding: 20px;
            border-radius: 8px;
            display: none;
        }

        .result.success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }

        .result.error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }

        .result h3 {
            margin-bottom: 10px;
        }

        .loading {
            display: none;
            text-align: center;
            margin-top: 20px;
        }

        .spinner {
            border: 3px solid #f3f3f3;
            border-top: 3px solid #667eea;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 0 auto;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .row {
            display: flex;
            gap: 15px;
        }

        .row .form-group {
            flex: 1;
        }

        @media (max-width: 768px) {
            .row {
                flex-direction: column;
            }
            
            .container {
                padding: 30px 20px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🔬 传感器数据分析系统</h1>
            <p>基于机器学习的通信格式预测</p>
        </div>

        <form id="analysisForm">
            <div class="form-group">
                <label for="transport-protocol">传输协议</label>
                <select id="transport-protocol" name="transport-protocol" required>
                    <option value="">请选择传输协议</option>
                    <option value="TCP">TCP</option>
                    <option value="UDP">UDP</option>
                    <option value="HTTP">HTTP</option>
                    <option value="MQTT">MQTT</option>
                    <option value="CoAP">CoAP</option>
                    <option value="LoRa">LoRa</option>
                    <option value="Zigbee">Zigbee</option>
                </select>
            </div>

            <div class="form-group">
                <label for="measure-type">测量种类</label>
                <select id="measure-type" name="measure-type" required>
                    <option value="">请选择测量种类</option>
                    <option value="温度">温度</option>
                    <option value="湿度">湿度</option>
                    <option value="压力">压力</option>
                    <option value="流量">流量</option>
                    <option value="电压">电压</option>
                    <option value="电流">电流</option>
                    <option value="功率">功率</option>
                    <option value="位移">位移</option>
                    <option value="速度">速度</option>
                    <option value="加速度">加速度</option>
                </select>
            </div>

            <div class="row">
                <div class="form-group">
                    <label for="max-measure">最高测量值</label>
                    <input type="number" id="max-measure" name="max-measure" step="0.01" required>
                </div>
                <div class="form-group">
                    <label for="min-measure">最低测量值</label>
                    <input type="number" id="min-measure" name="min-measure" step="0.01" required>
                </div>
            </div>

            <div class="form-group">
                <label for="measure-unit">测量单位</label>
                <select id="measure-unit" name="measure-unit" required>
                    <option value="">请选择测量单位</option>
                    <option value="℃">℃ (摄氏度)</option>
                    <option value="℉">℉ (华氏度)</option>
                    <option value="%">% (百分比)</option>
                    <option value="Pa">Pa (帕斯卡)</option>
                    <option value="kPa">kPa (千帕)</option>
                    <option value="MPa">MPa (兆帕)</option>
                    <option value="L/min">L/min (升/分钟)</option>
                    <option value="m³/h">m³/h (立方米/小时)</option>
                    <option value="V">V (伏特)</option>
                    <option value="mV">mV (毫伏)</option>
                    <option value="A">A (安培)</option>
                    <option value="mA">mA (毫安)</option>
                    <option value="W">W (瓦特)</option>
                    <option value="kW">kW (千瓦)</option>
                    <option value="mm">mm (毫米)</option>
                    <option value="cm">cm (厘米)</option>
                    <option value="m">m (米)</option>
                    <option value="m/s">m/s (米/秒)</option>
                    <option value="km/h">km/h (千米/小时)</option>
                    <option value="m/s²">m/s² (米/秒²)</option>
                </select>
            </div>

            <div class="form-group">
                <label for="current-measure">当前测量值</label>
                <input type="number" id="current-measure" name="current-measure" step="0.01" required>
            </div>

            <button type="submit" class="submit-btn" id="submitBtn">
                🔍 开始分析
            </button>
        </form>

        <div class="loading" id="loading">
            <div class="spinner"></div>
            <p>正在分析数据，请稍候...</p>
        </div>

        <div class="result" id="result">
            <h3>📊 分析结果</h3>
            <p id="resultText"></p>
        </div>
    </div>

    <script>
        document.getElementById('analysisForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const formData = new FormData(this);
            const params = new URLSearchParams();
            
            for (let [key, value] of formData.entries()) {
                params.append(key, value);
            }
            
            // 显示加载状态
            document.getElementById('loading').style.display = 'block';
            document.getElementById('result').style.display = 'none';
            document.getElementById('submitBtn').disabled = true;
            document.getElementById('submitBtn').textContent = '分析中...';
            
            try {
                const response = await fetch(`/analysis?${params.toString()}`);
                const data = await response.json();
                
                const resultDiv = document.getElementById('result');
                const resultText = document.getElementById('resultText');
                
                if (data.code === 200) {
                    resultDiv.className = 'result success';
                    resultText.innerHTML = `
                        <strong>推荐的通信格式：</strong><br>
                        ${data.prediction}
                    `;
                } else {
                    resultDiv.className = 'result error';
                    resultText.textContent = '分析失败，请检查输入数据并重试。';
                }
                
                resultDiv.style.display = 'block';
                
            } catch (error) {
                console.error('Error:', error);
                const resultDiv = document.getElementById('result');
                const resultText = document.getElementById('resultText');
                
                resultDiv.className = 'result error';
                resultText.textContent = '网络错误，请稍后重试。';
                resultDiv.style.display = 'block';
            } finally {
                // 隐藏加载状态
                document.getElementById('loading').style.display = 'none';
                document.getElementById('submitBtn').disabled = false;
                document.getElementById('submitBtn').textContent = '🔍 开始分析';
            }
        });

        // 表单验证：确保最高测量值大于最低测量值
        document.getElementById('max-measure').addEventListener('input', validateRange);
        document.getElementById('min-measure').addEventListener('input', validateRange);

        function validateRange() {
            const maxValue = parseFloat(document.getElementById('max-measure').value);
            const minValue = parseFloat(document.getElementById('min-measure').value);
            
            if (maxValue && minValue && maxValue <= minValue) {
                document.getElementById('max-measure').setCustomValidity('最高测量值必须大于最低测量值');
            } else {
                document.getElementById('max-measure').setCustomValidity('');
            }
        }

        // 根据测量种类自动调整单位选项
        document.getElementById('measure-type').addEventListener('change', function() {
            const measureType = this.value;
            const unitSelect = document.getElementById('measure-unit');
            
            // 清空当前选项
            unitSelect.innerHTML = '<option value="">请选择测量单位</option>';
            
            const unitOptions = {
                '温度': ['℃', '℉'],
                '湿度': ['%'],
                '压力': ['Pa', 'kPa', 'MPa'],
                '流量': ['L/min', 'm³/h'],
                '电压': ['V', 'mV'],
                '电流': ['A', 'mA'],
                '功率': ['W', 'kW'],
                '位移': ['mm', 'cm', 'm'],
                '速度': ['m/s', 'km/h'],
                '加速度': ['m/s²']
            };
            
            if (unitOptions[measureType]) {
                unitOptions[measureType].forEach(unit => {
                    const option = document.createElement('option');
                    option.value = unit;
                    option.textContent = unit;
                    unitSelect.appendChild(option);
                });
            }
        });
    </script>
</body>
</html>